import {Page} from '@/base/Page';
import {Order} from '@/base/model/Order';
import {isEmpty} from '@/base/util/Strings';
import {getOrderList} from '@/base/api/OrderApi';
import {getToday} from '@/base/util/tools';

class Form {
  createBegin = '';
  createEnd = '';
  phone = '';
  date = [];
  orderNum = '';
  orderState = '';
  sortDirection = 'DESC';
  sortList= [
    {
      value: 'ASC',
      label: '下单时间升序'
    },
    {
      value: 'DESC',
      label: '下单时间降序'
    }
  ]
}

export class OrderBloc {
  form = new Form();
  paged: Page<Order> = new Page<Order>([], 0, 1, 15, 0, []);
  loading = false;
  stateList = ['查看全部', '待发货', '待收货', '已收货', '已取消', '仅退款', '退货', '换货'];
  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.date[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.date[1].getTime() + 24 * 60 * 60 * 1000 || '';
    }
    this.loading = true;
    const stage = this.stateList.indexOf(this.form.orderState) === -1 ? 0 : this.stateList.indexOf(this.form.orderState);
    return getOrderList({
      pageIndex,
      pageSize,
      phone: this.form.phone,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd,
      orderNum: this.form.orderNum,
      orderState: stage,
      sortDirection: this.form.sortDirection
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }
  getStatus(OrderStage) {
    switch (OrderStage) {
      case '1' : return '待发货';
      case '2' : return '待收货';
      case '3' : return '已收货';
      case '4' : return '已取消';
      case '5' : return '仅退款';
      case '6' : return '退货';
      case '7' : return '换货';
      default : return '';
    }
  }
}
